package cn.itsource.order.service.impl;

import cn.itsource.config.RedisService;
import cn.itsource.order.domain.TUserAddress;
import cn.itsource.order.mapper.UserAddressMapper;
import cn.itsource.order.service.IUserAddressService;
import cn.itsource.user.domain.TLogininfo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
@Transactional
public class UserAddressServiceImpl implements IUserAddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public List<TUserAddress> findAllByUserId(String token) {
        //1. 根据token从redis中查询用户的登录信息
        // TODO 需要优化，在t_logininfo表中，添加2个字段：salt 和 user_id(已经搞定)
        TLogininfo logininfo = redisService.getKeyObjectValue(token, TLogininfo.class);
        //2. 根据用户的ID查询用户的地址列表
        Example example = new Example(TUserAddress.class);
        example.and().andEqualTo("userId", logininfo.getLoginId());

        return userAddressMapper.selectByExample(example);
    }
}
